using System;
using System.Collections.Generic;
using System.Text;
using ElteIk.RoboSoccer.Core;

namespace ElteIk.RoboSoccer.UnitTests
{
    class GameEventLogger
    {
        public const string GoalShootEvent = "GoalShoot";
        public const string GameStartedEvent = "GameStarted";
        public const string GameStoppedEvent = "GameStopped";
        public const string TimeOutEvent = "TimeOut";

        private Dictionary<string, int> eventHandlerInvocationsCount = new Dictionary<string, int>();

        public int GetInvocationTimes(string eventName)
        {
            int cnt = 0;
            eventHandlerInvocationsCount.TryGetValue(eventName, out cnt);
            return cnt;
        }

        public void LogEvent(string name)
        {
            // If there is no event registered with such name
            // Let more events log be identified by the same name
            if (!eventHandlerInvocationsCount.ContainsKey(name))
                eventHandlerInvocationsCount.Add(name, 0);

            ++eventHandlerInvocationsCount[name];
        }

        public static GameEventLogger CreateRoboSoccerGameEventLogger(RoboSoccerGame game)
        {
            GameEventLogger logger = new GameEventLogger();
            game.GoalShoot += delegate(object sender, RoboSoccerGame.GoalEventArgs e) { logger.LogEvent(GoalShootEvent); };
            game.GameStarted += delegate(object sender, EventArgs e) { logger.LogEvent(GameStartedEvent); };
            game.GameStopped += delegate(object sender, EventArgs e) { logger.LogEvent(GameStoppedEvent); };
            game.TimeOut += delegate(object sender, EventArgs e) { logger.LogEvent(TimeOutEvent); };
            return logger;
        }
    }
}
